//
//  FQDColumnHeaderAuthorCell.m
//  FakeQDaily
//
//  Created by mewxiv on 9/23/16.
//  Copyright © 2016 Xensius. All rights reserved.
//

#import "FQDColumnHeaderAuthorCell.h"
#import "FQDColumnCrossLineLabel.h"
#import "FQDColumnUsersView.h"
#import "FQDRestModel.h"

#import "Masonry.h"
#import "UIImageView+WebCache.h"

@interface FQDColumnHeaderAuthorCell ()

@property (nonatomic, strong) FQDColumnCrossLineLabel *lineLabel;
@property (nonatomic, strong) UIImageView *avatarImageView;
@property (nonatomic, strong) UILabel *authorNamelabel;
@property (nonatomic, strong) UILabel *authorDescLabel;
@property (nonatomic, strong) FQDColumnUsersView *multiAuthorsView;
@property (nonatomic, strong) UILabel *featureAuthorNameLabel;
@property (nonatomic, strong) UILabel *featureAuthorDescLabel;

@end

@implementation FQDColumnHeaderAuthorCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self prepareCell];
        [self prepareCellConstraints];
    }
    return self;
}

- (void)prepareCell
{
    self.backgroundColor = [UIColor whiteColor];
    
    CGSize viewSize = self.bounds.size;
    CGRect lineLabelFrame = CGRectMake(10, 0, viewSize.width - 20, 41);
    _lineLabel = [[FQDColumnCrossLineLabel alloc] initWithFrame:lineLabelFrame];
    _lineLabel.labelText = @"本栏目由以下记者主持";
    [self.contentView addSubview:_lineLabel];
    
    _avatarImageView = [[UIImageView alloc] init];
    _avatarImageView.contentMode = UIViewContentModeScaleAspectFill;
    _avatarImageView.clipsToBounds = YES;
    _avatarImageView.layer.cornerRadius = 22.5f;
    [self.contentView addSubview:_avatarImageView];
    
    _authorDescLabel = [[UILabel alloc] init];
    _authorDescLabel.font = FQDRegularFontWithSize(12.0f);
    _authorDescLabel.textColor = RGB(42, 42, 42);
    _authorDescLabel.textAlignment = NSTextAlignmentLeft;
    _authorDescLabel.numberOfLines = 0;
    _authorDescLabel.preferredMaxLayoutWidth = 170.0f;
    [self.contentView addSubview:_authorDescLabel];
    
    _authorNamelabel = [[UILabel alloc] init];
    _authorNamelabel.font = FQDBoldFontWithSize(12.0f);
    _authorNamelabel.textColor = RGB(42, 42, 42);
    _authorNamelabel.textAlignment = NSTextAlignmentCenter;
    _authorNamelabel.numberOfLines = 0;
    _authorNamelabel.preferredMaxLayoutWidth = 55.0f;
    [self.contentView addSubview:_authorNamelabel];
    
    _multiAuthorsView = [[FQDColumnUsersView alloc] init];
    [self.contentView addSubview:_multiAuthorsView];
    
    _featureAuthorNameLabel = [[UILabel alloc] init];
    _featureAuthorNameLabel.font = FQDBoldFontWithSize(12.0f);
    _featureAuthorNameLabel.textColor = RGB(42, 42, 42);
    _featureAuthorNameLabel.textAlignment = NSTextAlignmentCenter;
    _featureAuthorNameLabel.numberOfLines = 0;
    _featureAuthorNameLabel.preferredMaxLayoutWidth = 320.0f;
    [self.contentView addSubview:_featureAuthorNameLabel];
    
    _featureAuthorDescLabel = [[UILabel alloc] init];
    _featureAuthorDescLabel.font = FQDRegularFontWithSize(12.0f);
    _featureAuthorDescLabel.textColor = RGB(42, 42, 42);
    _featureAuthorDescLabel.textAlignment = NSTextAlignmentCenter;
    _featureAuthorDescLabel.numberOfLines = 0;
    _featureAuthorDescLabel.preferredMaxLayoutWidth = 236.0f;
    [self.contentView addSubview:_featureAuthorDescLabel];
}

- (void)prepareCellConstraints
{
    WeakSelf;
    [_lineLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.contentView);
        make.left.equalTo(weakSelf.contentView).offset(10);
        make.right.equalTo(weakSelf.contentView).offset(-10);
        make.height.equalTo(@41);
    }];
    
    [_avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.lineLabel.mas_bottom);
        make.left.equalTo(weakSelf.contentView).offset(25);
        make.size.equalTo(@45);
    }];
    
    [_authorDescLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.avatarImageView);
        make.left.equalTo(weakSelf.avatarImageView.mas_right).offset(10);
        make.right.equalTo(weakSelf.contentView).offset(-25);
        make.bottom.lessThanOrEqualTo(weakSelf.authorNamelabel);
    }];
    
    [_authorNamelabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.avatarImageView.mas_bottom).offset(6.5);
        make.bottom.equalTo(weakSelf.contentView);
        make.centerX.equalTo(weakSelf.avatarImageView);
    }];
    
    [_multiAuthorsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.contentView).offset(25);
        make.right.equalTo(weakSelf.contentView).offset(-25);
        make.top.equalTo(weakSelf.lineLabel.mas_bottom);
    }];
    
    [_featureAuthorNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.multiAuthorsView.mas_bottom);
        make.left.and.right.equalTo(weakSelf.contentView);
    }];
    
    [_featureAuthorDescLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.featureAuthorNameLabel.mas_bottom);
        make.left.equalTo(weakSelf.contentView).offset(42);
        make.right.equalTo(weakSelf.contentView).offset(-42);
        make.bottom.lessThanOrEqualTo(weakSelf.contentView);
    }];
}

- (void)setAuthors:(NSArray *)authors
{
    _authors = authors;
    
    NSInteger authorCount = [authors count];
    if (authorCount == 1) {
        self.avatarImageView.hidden = NO;
        self.authorNamelabel.hidden = NO;
        self.authorDescLabel.hidden = NO;
        self.multiAuthorsView.hidden = YES;
        self.featureAuthorNameLabel.hidden = YES;
        self.featureAuthorDescLabel.hidden = YES;
        
        FQDAuthor *author = [authors firstObject];
        NSURL *avatarURL = [NSURL URLWithString:author.avatar];
        [self.avatarImageView sd_setImageWithURL:avatarURL];
        
        self.authorNamelabel.text = author.name;
        self.authorDescLabel.text = author.desc;
    } else if (authorCount > 1) {
        self.avatarImageView.hidden = YES;
        self.authorNamelabel.hidden = YES;
        self.authorDescLabel.hidden = YES;
        self.multiAuthorsView.hidden = NO;
        self.featureAuthorNameLabel.hidden = NO;
        self.featureAuthorDescLabel.hidden = NO;
        
        FQDAuthor *author = [authors firstObject];
        self.multiAuthorsView.users = authors;
        self.multiAuthorsView.selectedUserId = author.ID;
        self.featureAuthorNameLabel.text = author.name;
        self.featureAuthorDescLabel.text = author.desc;
        [self setNeedsLayout];
    }
}

+ (NSString *)reuseId
{
    return @"FQDColumnHeaderAuthorCell";
}

@end
